package com.linln.admin.mapres.controller;

import com.linln.admin.mapres.validator.MapresValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.jpahelper.BaseCriteria;
import com.linln.common.jpahelper.Criteria;
import com.linln.common.jpahelper.Restrictions;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.utils.StringUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.jwt.annotation.JwtPermissions;
import com.linln.component.jwt.utlis.JwtUtil;
import com.linln.component.thymeleaf.utility.DictUtil;
import com.linln.modules.mapres.domain.Mapres;
import com.linln.modules.mapres.service.MapresService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author williham
 * @date 2020/08/04
 */
@Controller
@RequestMapping("/api/mapres")
public class MapresController {

    @Autowired
    private MapresService mapresService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("api:mapres:index")
    public String index(Model model, Mapres mapres) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("province", match -> match.contains())
                .withMatcher("city", match -> match.contains());

        // 获取数据列表
        Example<Mapres> example = Example.of(mapres, matcher);
        Page<Mapres> list = mapresService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/api/mapres/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("mapres:mapres:add")
    public String toAdd() {
        return "/api/mapres/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("mapres:mapres:edit")
    public String toEdit(@PathVariable("id") Mapres mapres, Model model) {
        model.addAttribute("mapres", mapres);
        return "/api/mapres/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"mapres:mapres:add", "mapres:mapres:edit"})
    @ResponseBody
    public ResultVo save(@Validated MapresValid valid, Mapres mapres) {
        // 复制保留无需修改的数据
        if (mapres.getId() != null) {
            Mapres beMapres = mapresService.getById(mapres.getId());
            EntityBeanUtil.copyProperties(beMapres, mapres);
        }

        // 保存数据
        mapresService.save(mapres);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("mapres:mapres:detail")
    public String toDetail(@PathVariable("id") Mapres mapres, Model model) {
        model.addAttribute("mapres",mapres);
        return "/api/mapres/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("mapres:mapres:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (mapresService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }

    /**
     * 设置LayerId
     */
    @GetMapping("/settinglayerid/{url}")
    @RequiresPermissions("mapres:mapres:settinglayerid")
    public String settingLayerId(@PathVariable("url") String url, Model model) {
        model.addAttribute("url", url);
        return "/api/mapres/settinglayerid";
    }

    /**
     * 查询所有资源---未分页
     */
    @GetMapping("/searchAllRes")
    @JwtPermissions
    @ResponseBody
    public ResultVo searchAllRes() {
        Criteria<Mapres> criteria=new BaseCriteria<>();
//        if(id!=null){
//            criteria.addAnd(Restrictions.eq("id",id));
//        }
//        if(docNo!=null){
//            criteria.addAnd(Restrictions.eq("docNo",docNo));
//        }
//        if(idNo!=null){
//            criteria.addAnd(Restrictions.eq("idNo",idNo));
//        }
        List<Mapres> documents = mapresService.findAll(criteria);

        return ResultVoUtil.success(documents);
    }


}